package com.gnerv.battle.boot.module.base.business.impl;

import com.gnerv.battle.boot.module.base.model.bo.DictItemBO;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.module.base.business.DictItemBusiness;
import com.gnerv.battle.boot.module.base.model.dto.DictItemCreateDTO;
import com.gnerv.battle.boot.module.base.model.dto.DictItemDTO;
import com.gnerv.battle.boot.module.base.model.dto.DictItemUpdateDTO;
import com.gnerv.battle.boot.module.base.service.DictItemService;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictItemBusinessImpl implements DictItemBusiness {

    private final DictItemService dictItemService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(DictItemCreateDTO dictItemCreateDTO) {
        DictItemBO dictItemBO = dictItemCreateDTO.toDictItemBO();
        dictItemBO.setUkId(UUIDTools.lower());
        dictItemService.create(dictItemBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        dictItemService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(DictItemUpdateDTO dictItemUpdateDTO) {
        dictItemService.updateByUkId(dictItemUpdateDTO.toDictItemBO());
        return RestResult.ok();
    }

    @Override
    public RestResult<DictItemDTO> selectByUkId(String ukId) {
        return RestResult.ok(dictItemService.selectByUkId(ukId));
    }

    @Override
    public RestResult<List<DictItemDTO>> listByDictUkId(String dictUkId) {
        return RestResult.ok(dictItemService.listByDictUkId(dictUkId));
    }

}
